package com.example.teliaCellInfo;

import android.os.Bundle;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.LocationProvider;

public class LocationCoordinates {
    
    public LocationManager locationManager;
    public LocationProvider lProvider;
    
    private double latitude = 0;
    private double longitude = 0;
    
    public double getLatitudeDouble(){
    	return this.latitude;
    }
    
    public double getLongitudeDouble(){
    	return this.longitude;
    }
    
    public String getLatitude(){
    	if (latitude == 0){
    		return "Waiting...";
    	}
    	String ret = Double.toString(this.latitude);
            return ret;
    }
    
    public String getLongitude(){
    	if (longitude == 0){
    		return "Waiting...";
    	}
    	String ret = Double.toString(this.longitude);
            return ret;
    }
    
    public LocationCoordinates (Context context){
            locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
            lProvider = locationManager.getProvider(LocationManager.NETWORK_PROVIDER);
    }       

    private final LocationListener listener = new LocationListener(){

            @Override
            public void onLocationChanged(Location location) {
                    longitude = location.getLongitude();
                    latitude = location.getLatitude();
                    stop();
            }

            @Override
            public void onProviderDisabled(String provider) {
                    // TODO LocationCoordinates/onProviderDisabled -Ignore for now
                    
            }

            @Override
            public void onProviderEnabled(String provider) {
                    // TODO LocationCoordinates/onProviderEnabled -Ignore for now
                    
            }

            @Override
            public void onStatusChanged(String provider, int status, Bundle extras) {
                    // TODO LocationCoordinates/onStatusChanged -Ignore for now
                    
            }
            
    };
    
    public void start(){          
            locationManager.requestLocationUpdates(lProvider.getName(), 100, 0, listener);
    }
    
    public void stop(){
            locationManager.removeUpdates(listener);
    }
}
